package com.exam.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.exam.exception.ClassesIsExistException;
import com.exam.mapper.ClassesMapper;
import com.exam.mapper.MajorMapper;
import com.exam.pojo.Classes;
import com.exam.pojo.Major;
import com.exam.service.ClassesService;

@Service("classesServiceImpl")
public class ClassesServiceImpl implements ClassesService {

	@Resource(name="classesMapper")
	private ClassesMapper classesMapper;
	@Resource(name="majorMapper")
	private MajorMapper majorMapper;
	
	/**
	 * 通过专业名称获取班级列表 
	 * @param majorName 专业名称
	 * @return List<Classes> 班级列表
	 */
	@Override
	public List<Classes> getClassesByMajorName(String majorName) {
		Major major = majorMapper.getMajorByName(majorName);	
		return classesMapper.getClassesByMajorId(major.getId());
	}

	/**
	 * 通过专业编号获取班级列表
	 * @param majorId 专业编号
	 * @return 班级列表
	 */
	public List<Classes> getClassesByMajorId(Integer majorId) {
		return classesMapper.getClassesByMajorId( majorId );
	}
	
	/**
	 * 添加班级 
	 * @param majorName 专业名称
	 * @param classesName 班级名称
	 * @return void
	 */
	@Override
	public void addClasses(String majorName,String classesName) throws ClassesIsExistException {
		Map map = new HashMap();
		Major major = majorMapper.getMajorByName(majorName); 
		map.put("majorId",major.getId());
		System.out.println("majorId="+major.getId());
		map.put("classesName",classesName);
		if(null != classesMapper.getClassesByMajorIdAndName(map)) { //如果班级已存在
			throw new ClassesIsExistException();
		} else {
			classesMapper.addClasses(map);
		}
	}
	
	/**
	 * 更新班级
	 * @param majorName 专业名称
	 * @param newName 新名称
	 * @param classesName 班级名称
	 * @return void
	 * @throws ClassesIsExistException 班级名称已存在异常
	 */
	@Override  
	public void updateClasses(String majorName,String newName,String classesName) throws ClassesIsExistException {
		Map map = new HashMap();
		Major major = majorMapper.getMajorByName(majorName); 
		map.put("majorId",major.getId());
		map.put("classesName",newName);
		if(null != classesMapper.getClassesByMajorIdAndName(map)) { //如果班级已存在
			throw new ClassesIsExistException();
		} else {
			Map map1 = new HashMap();
			map1.put("classesName",classesName);
			map1.put("majorId",major.getId());
			map1.put("classesId",classesMapper.getClassesByMajorIdAndName(map1).getId());
			map1.put("newName",newName);
			classesMapper.updateClasses(map1);
		}
	}

	/**
	 * 删除班级
	 * @param majorName 专业名称
	 * @param classesName 班级名称
	 * @return void
	 */
	@Override
	public void deleteClasses(String majorName,String classesName) {
		Map map = new HashMap();
		Major major = majorMapper.getMajorByName(majorName);
		map.put("majorId",major.getId());
		map.put("classesName",classesName);
		Classes classes = classesMapper.getClassesByMajorIdAndName(map);
		System.out.println(classes.getId());
		classesMapper.deleteClasses(classes.getId());
	}
	
	public Classes getClassesById(Integer classesId) {
		return classesMapper.getClassesById(classesId);
	}

}
